﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QLPM.DTO;

namespace QLPM.DAO
{
    public class DonGiaDAO
    {
        DataProvider provider;

        public DonGiaDAO()
        {
            provider = new DataProvider();
        }

        public List<DonGia> DanhSachDonGia(int maThuoc)
        {
            String sql = String.Format("select * from DONGIA where MaThuoc = {0}", maThuoc);
            provider = new DataProvider("DONGIA", sql);
            DataTable dt = provider;
            List<DonGia> ds = new List<DonGia>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DonGia dg = new DonGia();
                dg.MaDonGia = Convert.ToInt32(dt.Rows[i]["MaDonGia"]);
                dg.MaThuoc = Convert.ToInt32(dt.Rows[i]["MaThuoc"]);
                dg.MaDonVi = Convert.ToInt32(dt.Rows[i]["MaDonVi"]);
                dg.DonGia1 = Convert.ToInt32(dt.Rows[i]["DonGia"]);
                ds.Add(dg);
            }
            return ds;
        }

        public int ThemDonGia(DonGia dg)
        {
            String sql = String.Format("insert into DONGIA(MaThuoc, MaDonVi, DonGia) values({0}, {1}, {2})", dg.MaThuoc, dg.MaDonVi, dg.DonGia1);
            return provider.Excute_Non_Query(sql);
        }

        public int XoaDonGia(DonGia dg)
        {
            String sql = String.Format("delete from DONGIA where MaDonGia={0}", dg.MaDonGia);
            return provider.Excute_Non_Query(sql);
        }

        public int SuaDonGia(DonGia dg)
        {
            String sql = String.Format("update DONGIA set MaDonVi={0}, DonGia={1} where MaDonGia={2}", dg.MaDonVi, dg.DonGia1, dg.MaDonGia);
            return provider.Excute_Non_Query(sql);
        }
    }
}
